package com.chinasoft.set01.set02;

import java.util.Comparator;
import java.util.TreeSet;

public class Test02 {
    public static void main(String[] args) {
        // TreeSet(Comparator<? super E> comparator)
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = o1.getAge() - o2.getAge();
                result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
                return result;
            }
        });

        //创建学生对象
        Student s1 = new Student("zhangsan",28);
        Student s2 = new Student("lisi",27);
        Student s3 = new Student("wangwu",29);
        Student s4 = new Student("zhaoliu",26);
        Student s5 = new Student("dali",28);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);

        for (Student student:ts) {
            System.out.println( student);
        }
    }
}